<?php
/**
 * Supplier Retrieve Head
 */
class SRH extends AppModel {
    var $name = 'SRH';

    var $useTable = 'supplier_retrieve_heads';

    var $hasMany = array(
        'SR' => array(
            'foreignKey' => 'supplier_retrieve_head_id',
        )
    );

    var $belongsTo = array(
        'CRtSH' => array(
                'fields' => array('id', 'return_no', 'vendor_document_no'),
                'foreignKey' => 'cust_return_to_supplier_head_id',
        ),
            'Supplier', 
            'Customer' => array(
                'fields' => array('id', 'cust_no', 'name')
        ),
            'Site' => array(
                'fields' => array('id', 'site_no', 'name')
        ),
            'RS' => array(
                'foreignKey' => 'retrieve_status_id',
        ),
            'User' => array(
                'foreignKey' => 'collector_id',
                'fields' => array('username', 'name')
        )
    );

    var $validate = array(
            'retrieve_no' => array(
                'notEmpty' => array(
                    'rule' => array('notEmpty'),
                    'message' => 'Retrieve no. is required.',
                    'required' => true,
            ),
                'isUnique' => array(
                    'rule' => 'isUnique',
                    'message' => 'Retrieve no is already taken.',
                    'on' => 'create'
            ),
        ),
            'retrieve_date' => array(
                    'rule' => array('date', 'dmy'),
                    'message' => 'Retrieve date is required.',
                    'required' => true,
        ),
            'customer_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Customer is required.',
                    'required' => true,
        ),
            'site_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Site is required.',
                    'required' => true,
        ),
            'retrieve_status_id' => array(
                    'rule' => 'notEmpty',
                    'message' => 'Please select accept or decline.',
                    'required' => true,
        ),
    );

    function beforeSave() {
        if (isset($this->data['SRH']['retrieve_date'])) {
            $this->data['SRH']['retrieve_date'] = $this->dateFormatBeforeSave($this->data['SRH']['retrieve_date']);
        }
        return true;
    }

    function afterFind($results) {
        foreach ($results as &$custStockOutRetrieveHead) {
            if (isset($custStockOutRetrieveHead['SRH']['retrieve_date'])) {
                $custStockOutRetrieveHead['SRH']['retrieve_date'] = $this->dateFormatAfterFind($custStockOutRetrieveHead['SRH']['retrieve_date']);
            }
        }
        return $results;
    }
}
?>
